<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit" :selfUpdate="true">
        <a-form-item label="门店名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入联系人姓名'}]}]" />
        </a-form-item>
        <a-form-item label="星级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['score', { initialValue: 5, rules: [{required: true}] }]">
            <a-select-option :value="1">一星</a-select-option>
            <a-select-option :value="2">二星</a-select-option>
            <a-select-option :value="3">三星</a-select-option>
            <a-select-option :value="4">四星</a-select-option>
            <a-select-option :value="5">五星</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="门店图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <SelectImage
            :defaultList="record.image ? [record.image] : []"
            v-decorator="['cover']" />
        </a-form-item>
        <a-form-item
          label="店铺图片"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra="建议比例：16：9"
        >
          <SelectImage
            multiple
            :maxNum="10"
            :defaultList="record.previews ? record.previews : []"
            v-decorator="['imagesIds', {rules: [{required: true, message: '请至少上传1张店铺图片'}]}]"
          />
        </a-form-item>
        <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone', {rules: [{required: true, message: '请输入联系电话'}]}]" />
        </a-form-item>
        <a-form-item label="选择地区" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <SelectRegion
            placeholder="请选择省市区"
            v-decorator="['cascader', {rules: [{required: true, message: '请选择省市区'}]}]"
          />
        </a-form-item>
        <a-form-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['detail', {rules: [{required: true, message: '请输入详细地址'}]}]" />
        </a-form-item>
        <a-form-item label="坐标" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['coordinate']" />
        </a-form-item>
        <!--        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">-->
        <!--          <a-input-number-->
        <!--            :min="0"-->
        <!--            v-decorator="['sort', {initialValue: 100, rules: [{required: true, message: '请输入排序值'}]}]"-->
        <!--          />-->
        <!--        </a-form-item>-->
        <a-form-item label="地图选点" :labelCol="labelCol">
          <iframe
            id="map"
            src="/store/getpoint.html"
            width="912"
            height="610" />
        </a-form-item>
        <a-form-item class="mt-20" :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/store/address'
import { SelectImage, SelectRegion } from '@/components'
import { isEmptyObject } from '@/utils/util'
import $ from 'jquery'

export default {
  components: {
    SelectRegion,
    SelectImage
  },
  data () {
    return {
      // 对话框标题
      title: '编辑地址',
      // 标签布局属性
      labelCol: { span: 3 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      addressId: null,
      isLoading: false
    }
  },
  created () {
    this.initData()
  },
  methods: {

    // 初始化数据
    initData () {
      // 记录服务ID
      this.addressId = this.$route.query.addressId
      // 获取form所需的数据
      this.isLoading = true

      const { $nextTick } = this
      Api.detail(this.addressId)
        .then((res) => {
          if (res.status === 200 && !isEmptyObject(this.form.getFieldsValue())) {
            this.record = res.data.info
            $nextTick(() => {
              this.form.setFieldsValue(pick(this.record, ['detail', 'score', 'cascader', 'name', 'phone', 'coordinate', 'imagesIds']))
            })
          }
          this.isLoading = false
        })
    },

    /**
     * 显示对话框
     */
    edit (record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      record.cascader = [
        record.province_id,
        record.city_id,
        record.region_id
      ]
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['type', 'name', 'phone', 'cascader', 'detail', 'sort', 'imagesIds']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        values.coordinate = $('#coordinate').val()
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.edit({ addressId: this.addressId, form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 跳转到列表页
          setTimeout(() => {
            this.$router.push('./index')
          }, 1500)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
